<?php
header('Content-Type: text/html; charset=utf-8');

// HTTP_REFERER引用页不能为空（页面来源）
$referer_come_url = @$_SERVER['HTTP_REFERER'];
// slog($referer_come_url);
if (!isset($referer_come_url)) {
    // echo '禁止直接访问';
    exit();
}
// 解析 URL，返回其组成部分（scheme、host、port、user、pass、path、query在问号?之后、fragment在散列符号#之后）
$url_info = parse_url($referer_come_url);
$come_host = $url_info['host'];  // HTTP_REFERER页面中的域名
// $cur_domain = $_SERVER["SERVER_NAME"];  // 当前页面不带HTTP的域名部分
$cur_domain = 'servicewechat.com';
// 比较2个域名是否相同
// slog($cur_domain. $come_host);
if ($cur_domain != $come_host) {
    // echo '不同域名禁止访问';
    exit();
}

define('IN_TG', true);
//引入公共文件
require str_replace('api\add_visit_record', '', str_replace('api/add_visit_record', '', strtolower(dirname(__FILE__)))) . '/dream_include/common.inc.php';

$usr_lib_compare = _is_num(_get_isset('usr_lib_compare')); // 是否要去用户库比较并添加，2不去数据库比较添加直接写访问记录， 0去数据库比较有没有再写访问记录
$openid = _get_isset('uid'); // 用户的openid
$openid = htmlspecialchars($openid); 
$nickname = _get_isset('nickname'); // 用户昵称，这版小程序中没有获取用户昵称、头像
$nickname = htmlspecialchars($nickname); 

// 用户信息库start
$dbconn = new DbMysql();
if ($usr_lib_compare == 2 && $openid !== "") {
    // 查询是否不存在
    $query = "SELECT wx_openid FROM resource_wechat_user_library WHERE isdeleted=0 AND wx_openid='$openid' LIMIT 1";
    $result = $dbconn->sql_fetch_assoc($query);
    if ($result) {
        echo "y";
        // echo "数据已存在";
    } else {
        // echo "未找到数据";
        $_newUser = array();
        $_newUser['wx_openid'] = $openid;
        $_newUser['wx_nick_name'] = $nickname;
        // $_newUser['wx_gender'] = htmlspecialchars(_get_isset('gender'));
        // $_newUser['wx_language'] = htmlspecialchars(_get_isset('language'));
        // $_newUser['wx_city'] = htmlspecialchars(_get_isset('city'));
        // $_newUser['wx_province'] = htmlspecialchars(_get_isset('province'));
        // $_newUser['wx_country'] = htmlspecialchars(_get_isset('country'));
        $_newUser['wx_avatarurl'] = htmlspecialchars(_get_isset('avatarurl'));
        $_newUser['add_time'] = date("Y-m-d H:i:s");
        $_newUser['add_ip'] = getip();
        $_newUser['modify_time'] = date("Y-m-d H:i:s");
        $_newUser['modify_ip'] = getip();
        $_newUser['isdeleted'] = 0;
        $_newUser['check_onlycode'] = 'DREAMREN';
        $_newUser['valid_code'] = _ran6n();
        $result1 = $dbconn->insertArr($_newUser, "resource_wechat_user_library");
        echo "y";
    }
}
// 用户信息库end

// 访问记录库start
$_newinfo = array();
$_newinfo['PageID'] = _is_num(_get_isset('id'));
$_newinfo['OpenID'] = $openid;
$_newinfo['VisitPageUrl'] = htmlspecialchars(get_url(0));
$_newinfo['sourceUrl'] = htmlspecialchars($_sourceUrl);
$_newinfo['ItemText'] = htmlspecialchars(_get_isset('k'));
$_newinfo['miniURL'] = htmlspecialchars(_get_isset('miniurl'));
$_newinfo['record_title'] = htmlspecialchars(_get_isset('record_title'));
$_newinfo['device'] = htmlspecialchars(_get_isset('device'));
$_newinfo['Pixel'] = htmlspecialchars(_get_isset('px'));
$_newinfo['UserLoginIP'] = getip();
$ips = new IPtoRegion();
$_newinfo['IPtoCitySelf'] = $ips->show($_newinfo['UserLoginIP'], 2);
$_newinfo['HTTPUSERAGENT'] = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
// $_newinfo['SourceType'] = htmlspecialchars(_get_isset('esource')); // 没有传递
// $_newinfo['UserIdentify'] = htmlspecialchars(QHCRandomID()); // 同一用户多次访问会生成不同身份
$_newinfo['IsDeleted'] = 0;
// $_newinfo['InfoID'] = _is_num(_get_isset('infoid')); // 没有传递
// $_newinfo['ClassID'] = _is_num(_get_isset('classid')); // 没有传递
$_newinfo['AddDateTime'] = date("Y-m-d H:i:s");
$_newinfo['check_onlycode'] = 'DREAMREN';
$result2 = $dbconn->insertArr($_newinfo, "visit_record_source");
if ($result2) {
    if ($result2[0] > 0 && $result2[1] > 0) {
        //添加成功
        $dbconn->dbClose();
    } else {
        //添加失败
        $dbconn->dbClose();
    }
}


// //写入log日志，记录安全隐患
// function slog($logs)
// {
//     $toppath = "visit.txt";
//     // $toppath = "E:\safelog\dendn38n2b.txt";
//     $Ts = fopen($toppath, "ab+");
//     fwrite($Ts, $logs . "\r\n\r\n");
//     fclose($Ts);
// }


// /**
//  * QHCRandomID 获取生成的用户身份码
//  * @return string 编码字符串
//  */
// function QHCRandomID()
// {
//     if (isset($_COOKIE['CookQHCRandomID'])) {
//         //$GetQHC_RandomID = $_COOKIE['CookQHCRandomID'];
//         $_string = $_COOKIE['CookQHCRandomID'];
//         return $_string;
//         //echo "read：" . $GetQHC_RandomID;
//     } else {
//         $_string = str_replace(' ', '', getOS(1) . '-' . time() . '-' . _ran4n());
//         setcookie('CookQHCRandomID', $_string, time() + 15552000, "/");
//         //echo "cook：" . $GetQHC_RandomID;
//         return $_string;
//     }
// }

// /**
//  * curURL() 当前访问页面完整的Url地址
//  * @return string
//  */
// function curURL()
// {
//     $_string = _is_https() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//     return $_string;
// }


?>
